using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class Site : BaseItem
    {
        private Site() : base(){}

        private Site(Hashtable table) : base(table) { }

        private Site(Hashtable table, List<Site> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new SiteDal();
        }
        
        /// <summary>
        /// Returns an empty Site object
        /// </summary>
        public static Site CreateInstance()
        {
            return new Site();
        }

        private static Site GetItem(Hashtable table)
        {
            if (table != null)
            {
                return new Site(table);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Returns Collection of all Site objects
        /// </summary>
        public static List<Site> GetAll()
        {
            List<Site> tmp = new List<Site>();
            SiteDal dal = new SiteDal();
            Hashtable[] tables = dal.GetAll();
            foreach (Hashtable t in tables)
            {
                tmp.Add(new Site(t, tmp));
            }
            return tmp;
        }

        public static Site GetById(int id)
        {
            SiteDal dal = new SiteDal();
            return GetItem(dal.GetById(id));
        }

        public static Site GetByUrl(string siteUrl)
        {
            SiteDal dal = new SiteDal();
            return GetItem(dal.GetByUrl(siteUrl));
        }

        public static Site GetByRootUrl(string rootUrl)
        {
            SiteDal dal = new SiteDal();
            return GetItem(dal.GetByRootUrl(rootUrl));
        }

        public static Site GetByName(string name)
        {
            SiteDal dal = new SiteDal();
            return GetItem(dal.GetByName(name));
        }

        public static Site GetFromHttpRequest(System.Web.HttpRequest request)
        {
            string root = GetSiteRootFromHttpRequest(request);
            return GetByRootUrl(root);
        }

        public static string GetSiteRootFromHttpRequest(System.Web.HttpRequest request)
        {
            string siteRoot = request.ServerVariables["Server_Name"];
            if (request.ApplicationPath != "/")
                siteRoot += request.ApplicationPath;
            return siteRoot;
        }
        
        protected override void Load(Hashtable table)
        {
            id = (int)table[SiteDal.Fields.Id];
            name = (string)table[SiteDal.Fields.Name];
            root = (string)table[SiteDal.Fields.Root];
        }
    
        private int id = -1;
        /// <summary>
        /// Site Identifier
        /// </summary>
        public int Id
        {
            get { return id; }
        }

        private string name;

        /// <summary>
        /// Name of Site
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string root;

        /// <summary>
        /// Root Url of the Site
        /// </summary>
        public string Root
        {
            get { return root; }
            set { root = value; }
        }

        public new void Update()
        {
            base.Update();
        }

        protected override void SaveChanges(ref object concurencyId, Base dal)
        {
            ((SiteDal)dal).Update(ref id, name, root, ref concurencyId);
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((SiteDal)dal).Delete(id, concurencyId);
        }



 
    }
}
